package org.KeyCrafter.obj.type;

import org.KeyCrafter.ds.dict.*;

import java.util.Iterator;

import static org.KeyCrafter.ds.dict.Dict.DICT_OK;

public class DefaultSetType implements SetType{
    private static final DictType type = new MurmurHash2();
    private Dict dict;

    public DefaultSetType() {
        this.dict = new HashDict(type,null);
    }

    @Override
    public byte encoding() {
        return KeyCrafter_ENCODING_HT;
    }

    @Override
    public Dict content() {
        return this.dict;
    }

    @Override
    public boolean add(Object value) {
        return this.dict.add(value,null) == DICT_OK;
    }

    @Override
    public long size() {
        return this.dict.size();
    }

    @Override
    public Iterator initIterator() {
        DictIterator di = this.dict.getIterator();
        return new Iterator() {
            @Override
            public boolean hasNext() {
                return di.hasNext();
            }
            @Override
            public Object next() {
                Dict.Entry next = di.next();
                if (next != null) {
                    return next.getKey();
                } else {
                    return null;
                }
            }
        };
    }

    @Override
    public boolean isMember(Object value) {
        return this.dict.find(value) != null;
    }
}
